package com.marketlive.system.config.spring;

/*
(C) Copyright MarketLive. 2006. All rights reserved.
MarketLive is a trademark of MarketLive, Inc.
Warning: This computer program is protected by copyright law and international treaties.
Unauthorized reproduction or distribution of this program, or any portion of it, may result
in severe civil and criminal penalties, and will be prosecuted to the maximum extent
possible under the law.
*/

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.marketlive.system.config.IConfigurationManager;

import java.util.Properties;

import com.marketlive.system.config.ConfigurationBranch;
import com.marketlive.system.config.properties.PropertiesLoader;

/**
 * Injects the configuration into the context files.  The configuration must 
 * already be instantiated and is injected into this bean by the constructor.  It
 * is intended to be used in child contexts to the platform context, such as web
 * application contexts, where the child contexts want to substitute placeholders
 * in the configuration file.
 */
public class ConfigurationInjectedPlaceholderConfigurer extends PropertyPlaceholderConfigurer {

    /**
     * Logger
     */
    private static Log log = LogFactory.getLog(ConfigurationInjectedPlaceholderConfigurer.class);

    /**
     * Constructor requires the configuration manager.
     *
     * @param configurationManager the configuration manager to use.
     */
    public ConfigurationInjectedPlaceholderConfigurer(IConfigurationManager configurationManager) {
        log.info( "Loading configuration from:" + configurationManager);
        Properties allProps = new Properties();
        PropertiesLoader.loadProperties((ConfigurationBranch) configurationManager.getRoot(), allProps);
        super.setProperties(allProps);
    }

}

